package com.wmobile.utils;

import java.util.List;

import com.wellsoft.mobileoa.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PopListDialog<T> extends Dialog {
	Context baseActivity;
	String title;
	List<Integer> selectedIndex;
	boolean isMultiSelect;
	Handler mHandler;
	int dialogResult;
	public static int DialogResultOK = 1;
	public static int DialogResultCANCEL = 0;

	TextView titleTv;
	ListView dataListLv;
	Button confirmBtn;
	Button cancelBtn;
	PopListDialogAdapter<T> dataListAdapter;

	public List<Integer> getSelectedIndex() {
		return selectedIndex;
	}

	public int getDialogResult() {
		return dialogResult;
	}

	public PopListDialog(Context baseActivity, String title,
			List<Integer> selectedIndex,
			PopListDialogAdapter<T> dataListAdapter, boolean isMultiSelect) {
		super(baseActivity, R.style.popDialogTheme);
		this.title = title;
		this.baseActivity = baseActivity;
		this.selectedIndex = selectedIndex;
		this.dataListAdapter = dataListAdapter;
		this.isMultiSelect = isMultiSelect;
		onCreate();
	}

	public void onCreate() {
		setContentView(R.layout.poplistdialog);

		titleTv = (TextView) findViewById(R.id.titletv);
		titleTv.setText(title);

		dataListAdapter.setRadioButtonClickListener(radioButtonClickListener);
		dataListLv = (ListView) findViewById(R.id.datalistlv);
		dataListLv.setAdapter(dataListAdapter);
		dataListLv.setOnItemClickListener(onItemClickListerner);

		if (isMultiSelect) {
			confirmBtn = (Button) findViewById(R.id.confirmbtn);
			confirmBtn.setOnClickListener(onConfirmListener);
			confirmBtn.setVisibility(View.VISIBLE);
		}

		cancelBtn = (Button) findViewById(R.id.cancelbtn);
		cancelBtn.setOnClickListener(onCancelListener);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			onCancelListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	View.OnClickListener onConfirmListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultOK);
		}
	};

	View.OnClickListener onCancelListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultCANCEL);
		}
	};

	OnItemClickListener onItemClickListerner = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (isMultiSelect) {
				if (selectedIndex.contains(Integer.valueOf(arg2))) {
					selectedIndex.remove(Integer.valueOf(arg2));
				} else {
					selectedIndex.add(Integer.valueOf(arg2));
				}
				dataListAdapter.updateSelectedView(arg1, arg2);
			} else {
				selectedIndex.clear();
				selectedIndex.add(Integer.valueOf(arg2));
				endDialog(DialogResultOK);
			}
		}
	};
	
	View.OnClickListener radioButtonClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int row = Integer.parseInt(v.getTag().toString());
			selectedIndex.clear();
			selectedIndex.add(Integer.valueOf(row));
			endDialog(DialogResultOK);
		}
	};

	public void endDialog(int result) {
		dismiss();
		dialogResult = result;
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}

	public int showDialog() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message mesg) {
				 throw new RuntimeException();
			}
		};
		super.show();
		try {
			Looper.getMainLooper().loop();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return dialogResult;
	}

}
